package com.onjob.employment;

import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import util.dao.Pagination;

import com.onjob.photo.Photo;
import com.onjob.photo.PhotoDao;
import com.opensymphony.xwork2.ActionSupport;

public class ResumeAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ResumeDao resumeDao;
	private PersonInfo personInfo;
	private PersonInfoDao personInfoDao;
	private PhotoDao photoDao;
	private Resume resume;
	private String id;
	private Pagination<Resume> pagination;
	private int sign;
	private List<Resume> resumeList;
	private Photo photo;

	public void setResumeDao(ResumeDao resumeDao) {
		this.resumeDao = resumeDao;
	}

	public PersonInfo getPersonInfo() {
		return personInfo;
	}

	public void setPersonInfo(PersonInfo personInfo) {
		this.personInfo = personInfo;
	}

	public void setPersonInfoDao(PersonInfoDao personInfoDao) {
		this.personInfoDao = personInfoDao;
	}

	public void setResume(Resume resume) {
		this.resume = resume;
	}

	public Resume getResume() {
		return resume;
	}

	public String getId() {

		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String resumes() throws Exception {
		if (null == ServletActionContext.getRequest().getSession()
				.getAttribute("personInfoid")) {
			return INPUT;
		} else {
			this.setId(ServletActionContext.getRequest().getSession()
					.getAttribute("personInfoid").toString());
			this.setSign(resumeDao.findAllResumes(
					ServletActionContext.getRequest().getSession()
							.getAttribute("personInfoid").toString()).size());
			return SUCCESS;
		}

	}

	@Transactional(propagation = Propagation.REQUIRED)
	public String resumes_submit() throws Exception {

		this.resume.setDate(new Date());
		this.resume.setPersonInfo(this.personInfoDao
				.findById(ServletActionContext.getRequest().getSession()
						.getAttribute("personInfoid").toString()));
		this.resumeDao.saveOrUpdate(this.resume);
		ServletActionContext.getRequest().getSession().setAttribute(
				"resumeid",
				this.resumeDao.findResumeByTypes(this.resume.getName(),(ServletActionContext.getRequest().getSession()
						.getAttribute("personInfoid").toString()), "name","personInfo.id"));
		

		return SUCCESS;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public String showall() throws Exception {
		try {
			this.setResumeList(this.resumeDao
					.findAllResumes(ServletActionContext.getRequest()
							.getSession().getAttribute("personInfoid")
							.toString()));
		
		} catch (Exception e) {
			new RuntimeException("浏览信息有误");
		}

		return SUCCESS;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public String resume_update() throws Exception {

		this.resume = this.resumeDao.findById(this.getId());
		this.setId(this.resume.getPersonInfo().getId());

		return SUCCESS;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public String resume_deletes() throws Exception {
System.out.println(">>>>>>>>>>>>>>>>."+this.resume.getId()+"<<<<<<<<<<<<<<");
		this.resume = this.resumeDao.findById(this.resume.getId());
		if(null != this.photoDao.findByResumeId(this.resume.getId())){
			this.photoDao.delete(this.photoDao.findByResumeId(this.resume.getId()));
		}
		
		this.resumeDao.delete(resume);
		return SUCCESS;
	}

	public ResumeAction() {
		this.pagination = new Pagination<Resume>();
		this.pagination.setSize(100);
		this.pagination.setNo(1);
	}

	public Pagination<Resume> getPagination() {
		return pagination;
	}

	public void setPagination(Pagination<Resume> pagination) {
		this.pagination = pagination;
	}

	public PersonInfoDao getPersonInfoDao() {
		return personInfoDao;
	}

	public int getSign() {
		return sign;
	}

	public void setSign(int sign) {
		this.sign = sign;
	}

	public List<Resume> getResumeList() {
		return resumeList;
	}

	public void setResumeList(List<Resume> resumeList) {
		this.resumeList = resumeList;
	}

	public void setPhotoDao(PhotoDao photoDao) {
		this.photoDao = photoDao;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void deleteAll(Long id) {
		System.out.println(">>>>>>>>>>>>>>执行了这个方法");

		if (null != this.personInfoDao.findByType(id.toString(), "userId.id")) {
			this.personInfo = this.personInfoDao.findByType(id.toString(),
					"userId.id");
			if (null != this.resumeDao.findResumeByType(
					this.personInfo.getId(), "personInfo.id")) {
				this.resume = this.resumeDao.findResumeByType(this.personInfo
						.getId(), "personInfo.id");
				if (null != this.photoDao.findPhotoByType(this.resume.getId(),
						"resumeId.id")) {
					this.photoDao.delete(this.photoDao.findPhotoByType(
							this.resume.getId(), "resumeId.id"));
					this.resumeDao.delete(this.resume);
					this.personInfoDao.delete(this.personInfo);
				} else {
					this.resumeDao.delete(this.resume);
					this.personInfoDao.delete(this.personInfo);
				}
			} else {
				this.personInfoDao.delete(this.personInfo);
			}
		} else {
			// 直接删除uers
		}
	}

	public Photo getPhoto() {
		return photo;
	}

	public void setPhoto(Photo photo) {
		this.photo = photo;
	}

	public String findMyResume() throws Exception {

		return SUCCESS;
	}
}
